package code;

import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.util.ArrayList;

import java.util.Scanner;

public class supermarket {
    public static void main(String[] args) {
        System.out.println("-----欢迎来到成信大超市管理系统-----");
        ArrayList<user> list = new ArrayList<user>();
        user u = new user();
        u.userName = "jack";
        u.password = 1234;
        u.userId = 1001;
        list.add(u);
        printArray(list);
        homePage(list);
    }


    public  static void homePage(ArrayList<user> list){

        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("-----欢迎来到成信大超市注册登录管理系统-----");
            System.out.println("请输入你要执行的操作:");
            System.out.println("1.注册"+"   2.登录"+"   3.退出系统");
            int chooseNum = sc.nextInt();
            int idCount = 1001;
            switch (chooseNum){
                case 1:
                    userRegistration(list,idCount);
                    break;
                case 2:
                    if (list.isEmpty()){
                        System.out.println("没有注册用户,无法登陆,请先注册!!!");
                        break;
                    }
                    int isCorrect = userLogin(list);
                    if(isCorrect == 1){
                        secondPage();
                    }
                    break;
                case 3:
                    exit();
                    return;
                default:
                    System.out.println("输入有误,请重新输入正确编号:");
            }
        }
    }

    private static void secondPage(){
        ArrayList<goods> goodslist = new ArrayList<goods>();
        goods item1 = new goods();
        goods item2 = new goods();
        goods item3 = new goods();

        item1.goodsName = "苹果";item1.id=1001;item1.price=120.0;
        item2.goodsName = "香蕉";item2.id=1002;item2.price=130.0;
        item3.goodsName = "草莓";item3.id=1003;item3.price=140.0;

        goodslist.add(item1);
        goodslist.add(item2);
        goodslist.add(item3);
        printArr(goodslist);
        System.out.println("-----欢迎来到成信大超市货物管理系统-----");
        Scanner sc = new Scanner(System.in);
        int idCount = 1001;
        while (true) {
            System.out.println("1.查询货物" + " 2.添加货物" + " 3.删除货物"+"4.修改货物");
            System.out.println("5.退出成信大超市");
            System.out.println("请输入你要执行的操作:");
            int chooseNum = sc.nextInt();
            switch (chooseNum){
                case 1:
                    searchGoods(goodslist);
                    break;
                case 2:
                    addGoods(goodslist,idCount);
                    break;
                case 3:
                    deleteGoods(goodslist);
                    break;
                case 4:
                    updateGoods(goodslist);
                    break;
                case 5:
                    exit();
                    return;
                default:
                    System.out.println("输入编号有误,请重新输入:");
            }
        }
//        System.out.println("感谢使用诚信大超市,即将返回登录界面");
    }

    public static void userRegistration(ArrayList<user> list,int id){
        Scanner sc = new Scanner(System.in);
        user u1 = new user();

        while (true){
            int flag = 0;
            System.out.println("请输入你要注册的账户名称:");
            String name = sc.next();
            System.out.println("请输入你的密码:");
            int pass = sc.nextInt();

            for (int i=0;i<list.size();i++){
                if (name.equals(list.get(i).userName) && flag == 0){
                    System.out.println("你要注册的名称已被占用,请重新输入");
                    flag = 1;
                }
            }
            if (flag == 0) {
                u1.userName = name;
                u1.password = pass;
                while (true){
                    int temp = 0;
                    for (int i=0;i<list.size();i++){
                        if (id == list.get(i).userId){
                            temp = 1;
                            break;
                        }
                    }
                    if (temp == 0){
                        u1.userId = id;
                        break;
                    }
                    id++;
                }
                list.add(u1);
                System.out.println("恭喜你,注册成功!");
                printArray(list);
                break;
            }
        }
    }

    public  static int userLogin(ArrayList<user> list){
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入你的用户名:");
            String name = sc.next();
            if (name.equals("admin")){
                break;
            }
            System.out.println("请输入你的密码:");
            int pass = sc.nextInt();
            for(int i=0;i<list.size();i++){
                if (name.equals(list.get(i).userName) && pass == list.get(i).password){
                    System.out.println("登陆成功,即将进入系统");
                    return 1;
                }else{
                    System.out.println("用户名或密码输入错误!请重新输入:(如需退出登录系统,请将用户名输为admin)");
                }
            }
        }
        return 0;
    }

    private static void printArray(ArrayList<user> list) {
        for(int i=0;i<list.size();i++){
            System.out.print("账户名称:"+list.get(i).userName);
            System.out.print(" 账户密码:"+list.get(i).password);
            System.out.println(" 用户ID是:"+list.get(i).userId);
        }
    }

    private static void printArr(ArrayList<goods> list) {
        for(int i=0;i<list.size();i++){
            System.out.println("商品编号:"+list.get(i).id);
            System.out.println("商品名称:"+list.get(i).goodsName);
            System.out.println("商品价格:"+list.get(i).price);
        }
    }

    private static void exit(){
        System.out.println("欢迎再次使用成信大超市管理系统,再见");
    }

    private static void searchGoods(ArrayList<goods> list) {
        System.out.println("商品编号"+"  商品名称"+"  商品价格");
        for (int i=0;i<list.size();i++){
            System.out.print("  "+list.get(i).id);
            System.out.print("     "+list.get(i).goodsName);
            System.out.println("      "+list.get(i).price);
        }

        System.out.println("查询成功");
    }

    private static void addGoods(ArrayList<goods> list,int id){
        Scanner sc = new Scanner(System.in);
        goods item = new goods();
        int flag = 0;

        System.out.println("请输入水果名称:");
        String name = sc.next();
        while (true){
           // flag = 0;
            for (int i=0;i<list.size();i++){
                if (name.equals(list.get(i).goodsName)){
                    System.out.println("名称重复,请重新输入水果名称:");
                    name = sc.next();
                    flag = 0;
                    break;
                }else{
                    flag = 1;
                }
            }
            if (flag == 1){
                break;
            }

        }

        System.out.println("请输入水果价格:");
        double price = sc.nextDouble();


        while (true){
            flag = 0;
            for (int i=0;i<list.size();i++){
                if (id == list.get(i).id){
                    flag = 1;
                    break;
                }
            }
            if (flag == 0){
                item.id = id;
                break;
            }
            id++;
        }

        item.goodsName = name;
        item.price = price;

        list.add(item);
        printArr(list);
        System.out.println("添加成功");
    }

    private static void deleteGoods(ArrayList<goods> list){
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入你要删除的编号");
        int idNum = sc.nextInt();
        for (int i=0;i<list.size();i++){
            if (idNum == list.get(i).id){
                list.remove(i);
            }
        }
        System.out.println("删除成功");
    }

    private static void updateGoods(ArrayList<goods> list){
        Scanner sc = new Scanner(System.in);
        int flag = 0;
        System.out.println("请输入要修改的商品编号:");
        int idNum = sc.nextInt();
        while (true){
            for (int i=0;i<list.size();i++){
                if (idNum == list.get(i).id){
                    flag = 1;
                    break;
                }
            }
            if (flag == 1){
                break;
            }
            System.out.println("不存在此编号,请重新输入商品编号:");
            idNum = sc.nextInt();
        }

        while (true){
            System.out.println("1.修改(商品ID为:"+idNum+")商品名称");
            System.out.println("2.修改(商品ID为:"+idNum+")商品价格");
            System.out.println("3.修改(商品ID为:"+idNum+")商品编号");
            System.out.println("4.修改(商品ID为:"+idNum+")商品所有信息");
            System.out.println("5.退出修改系统");
            int chooseNum = sc.nextInt();
            switch (chooseNum){
                case 1:
                    updateName(list,idNum);
                    break;
                case 2:
                    updatePrice(list,idNum);
                    break;
                case 3:
                    updateIdNum(list,idNum);
                    return;
                case 4:
                    upadateAll(list,idNum);
                    return;
                case 5:
                    return;
                default:
                    System.out.println("输入编号有误,请重新输入:");
            }
        }
    }

    private static void updateName(ArrayList<goods> list,int id){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的名称:");
        String newName = sc.next();
        for (int i=0;i<list.size();i++){
            if (id == list.get(i).id){
                list.get(i).goodsName = newName;
            }
        }
        searchGoods(list);
        System.out.println("名称修改成功");
    }

    private static void updatePrice(ArrayList<goods> list,int id){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的价格:");
        double newPrice = sc.nextDouble();
        for (int i=0;i<list.size();i++){
            if (id == list.get(i).id){
                list.get(i).price = newPrice;
            }
        }
        searchGoods(list);
        System.out.println("价格修改成功");
    }

    private static void updateIdNum(ArrayList<goods> list,int id){
        Scanner sc = new Scanner(System.in);
        int flag = 1;
        System.out.println("请输入你要修改的ID:");
        int newId = sc.nextInt();

        while (true){
            flag = 1;
            for (int i=0;i<list.size();i++){
                if (newId == list.get(i).id){
                    System.out.println("ID重复,请重新输入你要修改的ID:");
                    newId = sc.nextInt();
                    flag = 0;
                    break;
                }
            }
            if (flag == 1){
                break;
            }
        }

        for (int i=0;i<list.size();i++){
            if (id == list.get(i).id){
                list.get(i).id = newId;
            }
        }
        searchGoods(list);
        System.out.println("ID修改成功");
    }

    private static void upadateAll(ArrayList<goods> list,int id){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的名称:");
        String newName = sc.next();
        System.out.println("请输入你要修改的价格:");
        double newPrice = sc.nextDouble();
        System.out.println("请输入你要修改的ID:");
        int newId = sc.nextInt();

        int flag;
        while (true){
            flag = 1;
            for (int i=0;i<list.size();i++){
                if (newId == list.get(i).id){
                    System.out.println("ID重复,请重新输入你要修改的ID:");
                    newId = sc.nextInt();
                    flag = 0;
                    break;
                }
            }
            if (flag == 1){
                break;
            }
        }
        for (int i=0;i<list.size();i++){
            if (id == list.get(i).id){
                list.get(i).goodsName = newName;
                list.get(i).price = newPrice;
                list.get(i).id = newId;
            }
        }
        searchGoods(list);
        System.out.println("全部数据修改成功");
    }

}
